package com.aco.party.web.controller.system;

import com.aco.party.enums.QuestionStatusEnum;
import com.aco.party.model.po.Question;
import com.aco.party.model.vo.QuestionParam;
import com.aco.party.model.vo.QuestionVo;
import com.aco.party.model.vo.common.DataGridEntry;
import com.aco.party.model.vo.common.Page;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.Response;
import com.aco.party.service.system.QuestionService;
import com.aco.party.service.system.MetaService;
import com.aco.party.web.command.system.EditQuestionCommand;
import com.aco.party.web.validator.system.EditSimpleInputValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Controller
@RequestMapping("/system")
public class ManageQuestionController extends BaseController {
    Logger logger = LoggerFactory.getLogger(ManageQuestionController.class);

    @Resource
    private QuestionService questionService;

    @Resource
    private MetaService metaService;

    private EditSimpleInputValidator validator = new EditSimpleInputValidator();

    private final String REDIRECTEDIT = "redirect:edit";

    @RequestMapping(value = {"/question", "/question/list"}, method = RequestMethod.GET)
    public String showManagePage(HttpServletResponse response) {
        return "system/manageQuestions";
    }

    @RequestMapping(value = "question/list", method = RequestMethod.POST)
    @ResponseBody
    public Page <QuestionVo> getListJson(HttpServletRequest request, DataGridEntry entry) {
        Page <QuestionVo> page = new Page<>();
        entry.webParamConvert();
        QuestionParam param = new QuestionParam();
        param.setPId(0L);
        List <QuestionVo> list = questionService.getByPage(new PageParam(entry.getOffset(), entry.getLimit()),param);
        page.setTotal(questionService.getTotal(param));
        page.setRows(list);
        return page;
    }

    @RequestMapping(value = "/question/edit", method = RequestMethod.GET)
    public String showEdit(HttpServletResponse response,@ModelAttribute EditQuestionCommand command, Model model) {
        if (command.getId() != null) {
            Question po = questionService.getByPrimaryKey(command.getId());
            if (po != null) {
                command.toCommand(po);
                QuestionParam param = new QuestionParam();
                param.setPId(po.getId());
                List<QuestionVo> remarks = questionService.getByPage(null, param);
                if(remarks != null)
                    model.addAttribute("remarks",remarks);
            }
            else {
                model.addAttribute("no_obj", true);
            }
        }
        return "system/editQuestion";
    }

    @RequestMapping(value = "/question/edit", method = RequestMethod.POST)
    public String edit(Model model, @ModelAttribute EditQuestionCommand command, BindingResult errors) {
        validator.validate(command, errors);
        setAtt(model);
        if (!errors.hasErrors()) {
            Question question = new Question();
                command.toPo(question);
            question.setUpdater(getCurrentUser().getUsername());
            questionService.update(question);
            model.addAttribute("no_error",true);
        }
        return "system/editQuestion";
    }

    @RequestMapping(value = "/question/add", method = RequestMethod.GET)
    public String showAdd(@ModelAttribute EditQuestionCommand command) {
        command.setStatus(QuestionStatusEnum.正在审核中.getDesc());
        return "system/editQuestion";
    }

    @RequestMapping(value = "/question/add", method = RequestMethod.POST)
    public String add(Model model, @ModelAttribute EditQuestionCommand command, BindingResult errors,RedirectAttributes attributes) {
        validator.validate(command, errors);
        setAtt(model);
        if (errors.hasErrors()) {
            return "system/editQuestion";
        }
        Question question = new Question();
        command.toPo(question);
        question.setUserId(getCurrentUser().getId());
        questionService.add(question);
        attributes.addAttribute("id",question.getId());
        attributes.addFlashAttribute("no_error",true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/question/delete",method = RequestMethod.POST)
    @ResponseBody
    public Response del(@RequestParam(required = true) Long id){
        logger.info("删除：" + id);
        questionService.delete(id);
        return new Response();
    }

    @RequestMapping(value = "/question/up",method = RequestMethod.POST)
    @ResponseBody
    public Response up(@RequestParam(required = true) Long id,Integer isUp){
        Question question = questionService.getByPrimaryKey(id);
        if(question != null){
            if(isUp != null && isUp == 1)
                question.setStatus(QuestionStatusEnum.推荐.getDesc());
            else
                question.setStatus(QuestionStatusEnum.显示.getDesc());
            question.setUpdater(getCurrentUser().getUsername());
            questionService.update(question);
        }
        return new Response();
    }

    private void setAtt(Model model){

    }

}
